package KiemTra;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class BaiTap002 extends JFrame{

	public BaiTap002(){
		setVisible(true);
		setSize(500, 160);
		
		JLabel td = new JLabel("CHƯƠNG TRÌNH ĐỔI TIỀN");
		JPanel p1 = new JPanel();
		p1.add(td);
		add(p1, BorderLayout.NORTH);
		
		final JRadioButton radUSD = new JRadioButton("USD");
		final JRadioButton radEUR = new JRadioButton("EUR");
		final JRadioButton radJPY = new JRadioButton("JPY");
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(radUSD);
		bg.add(radEUR);
		bg.add(radJPY);
		radUSD.setSelected(true);
		
		JPanel rad = new JPanel();
		rad.setLayout(new GridLayout(0,3));
		rad.add(radUSD);
		rad.add(radEUR);
		rad.add(radJPY);
		
		final JTextField jtf_Input = new JTextField();
		final JTextField jtf_Output = new JTextField();
		
		final JButton jbt_Tinh = new JButton("Thực hiện");
		final JButton jbt_Xoa = new JButton("Xóa");
		final JButton jbt_Thoat = new JButton("Thoát");		
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3,3));
		p.add(new JLabel("Tiền việt:"));p.add(jtf_Input);p.add(jbt_Tinh);
		p.add(new JLabel("Loại ngoại tệ:"));p.add(rad);p.add(jbt_Xoa);
		p.add(new JLabel("Kết quả:"));p.add(jtf_Output);p.add(jbt_Thoat);
		jtf_Input.setHorizontalAlignment(JTextField.RIGHT);
		jtf_Output.setHorizontalAlignment(JTextField.RIGHT);
		
		add(p);
		
		jbt_Thoat.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		jbt_Xoa.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtf_Input.setText(null);
				jtf_Output.setText(null);
				jbt_Tinh.setEnabled(true);
			}
		});
		
		jbt_Tinh.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try{
					int n_TienViet = Integer.parseInt(jtf_Input.getText());
					if(radUSD.isSelected()){
						int kq = n_TienViet/2;
						jtf_Output.setText(String.valueOf(kq));
					}else if(radEUR.isSelected()){
						int kq = n_TienViet/3;
						jtf_Output.setText(String.valueOf(kq));
					}else{
						int kq = n_TienViet/4;
						jtf_Output.setText(String.valueOf(kq));
					}
					
					jbt_Tinh.setEnabled(false);
				}catch (Exception ex) {
					// TODO: handle exception
					System.out.print(ex.getMessage());
				}
			}
		});
		
	}
	public static void main(String[] args) {
		new BaiTap002();
	}

}
